package com.nhim.cms.domain;

import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Version;

import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * ElementDefinition entity. @author Huy Lieu Ta
 */
@Entity
@Table(name = "nhim_element_definition", catalog = "nhim_cms")
public class ElementDefinition implements java.io.Serializable {

	// Fields

	/**
	 * 
	 */
	private static final long serialVersionUID = 794156435052021062L;
	private Integer id;
	private Boolean allowEdit;
	private String defaultValue;
	private Boolean isMetadata;
	private Boolean isSearchable;
	private String name;
	private String description;
	private String definitionText;
	private String elementType;
	private Integer version;
	private Set<ElementTemplate> elementTemplates = new HashSet<ElementTemplate>(
			0);
	private Set<ContentTypeElementDefinition> contentTypeElementDefinitions = new HashSet<ContentTypeElementDefinition>(
			0);

	// Constructors

	/** default constructor */
	public ElementDefinition() {
	}

	/** minimal constructor */
	public ElementDefinition(String name, String definitionText,
			String elementType) {
		this.name = name;
		this.definitionText = definitionText;
		this.elementType = elementType;
	}

	/** full constructor */
	public ElementDefinition(Boolean allowEdit, String defaultValue,
			Boolean isMetadata, Boolean isSearchable, String name,
			String description, String definitionText, String elementType,
			Set<ElementTemplate> elementTemplates,
			Set<ContentTypeElementDefinition> contentTypeElementDefinitions) {
		this.allowEdit = allowEdit;
		this.defaultValue = defaultValue;
		this.isMetadata = isMetadata;
		this.isSearchable = isSearchable;
		this.name = name;
		this.description = description;
		this.definitionText = definitionText;
		this.elementType = elementType;
		this.elementTemplates = elementTemplates;
		this.contentTypeElementDefinitions = contentTypeElementDefinitions;
	}

	// Property accessors
	@Id
	@GeneratedValue(strategy = IDENTITY)
	@Column(name = "id", unique = true, nullable = false)
	public Integer getId() {
		return this.id;
	}

	@Version
	@Column(name = "version")
	public Integer getVersion() {
		return this.version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "allow_edit")
	public Boolean getAllowEdit() {
		return this.allowEdit;
	}

	public void setAllowEdit(Boolean allowEdit) {
		this.allowEdit = allowEdit;
	}

	@Column(name = "default_value")
	public String getDefaultValue() {
		return this.defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	@Column(name = "is_metadata")
	public Boolean getIsMetadata() {
		return this.isMetadata;
	}

	public void setIsMetadata(Boolean isMetadata) {
		this.isMetadata = isMetadata;
	}

	@Column(name = "is_searchable")
	public Boolean getIsSearchable() {
		return this.isSearchable;
	}

	public void setIsSearchable(Boolean isSearchable) {
		this.isSearchable = isSearchable;
	}

	@Column(name = "name",unique=true, length = 100, nullable = false)
	public String getName() {
		return this.name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "description", length = 200)
	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Column(name = "definition_text", nullable = false)
	public String getDefinitionText() {
		return this.definitionText;
	}

	public void setDefinitionText(String definitionText) {
		this.definitionText = definitionText;
	}

	@Column(name = "element_type",length = 50, nullable = false)
	public String getElementType() {
		return this.elementType;
	}

	public void setElementType(String elementType) {
		this.elementType = elementType;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "elementDefinition")
	public Set<ElementTemplate> getElementTemplates() {		
		return this.elementTemplates;
	}

	public void setElementTemplates(Set<ElementTemplate> elementTemplates) {
		this.elementTemplates = elementTemplates;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "elementDefinition")
	public Set<ContentTypeElementDefinition> getContentTypeElementDefinitions() {
		return this.contentTypeElementDefinitions;
	}

	public void setContentTypeElementDefinitions(
			Set<ContentTypeElementDefinition> contentTypeElementDefinitions) {
		this.contentTypeElementDefinitions = contentTypeElementDefinitions;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("Id: ").append(getId()).append(", ");
		sb.append("Version: ").append(getVersion()).append(", ");
		sb.append("Description: ").append(getDescription()).append(", ");
		sb.append("Is Metadata: ").append(getIsMetadata()).append(", ");
		sb.append("Allow Edit: ").append(getAllowEdit()).append(", ");
		sb.append("Searchable: ").append(getIsSearchable()).append(", ");
		sb.append("Element Type: ").append(getElementType()).append(", ");
		sb.append("DefinitionText: ").append(getDefinitionText()).append(", ");
		return sb.toString();
	}

}